/*
 * Copyright (C) 2017 zhouyou(478319399@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shuyihui.rxhttp.utils

import com.shuyihui.rxhttp.utils.HttpLog.e
import java.nio.charset.Charset

/**
 *
 * 描述：http工具类
 */
object HttpUtil {
    val UTF8 = Charset.forName("UTF-8")
    fun createUrlFromParams(url: String, params: Map<String, String>): String {
        try {
            val sb = StringBuilder()
            sb.append(url)
            if (url.indexOf('&') > 0 || url.indexOf('?') > 0) sb.append("&") else sb.append("?")
            for ((key, urlValues) in params) {
                //对参数进行 utf-8 编码,防止头信息传中文
                //String urlValue = URLEncoder.encode(urlValues, UTF8.name());
                sb.append(key).append("=").append(urlValues).append("&")
            }
            sb.deleteCharAt(sb.length - 1)
            return sb.toString()
        } catch (e: Exception) {
            e(e.message)
        }
        return url
    }
}